package com.example.demoofimagedownloadapp.view;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.demoofimagedownloadapp.R;
import com.example.demoofimagedownloadapp.contract.MainContract;
import com.example.demoofimagedownloadapp.model.utils.DownloadAsyncTask;
import com.example.demoofimagedownloadapp.presenter.MainPresenter;
import com.example.demoofimagedownloadapp.view.utils.AnimationHelper;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ts
 */
public class MainActivity extends BaseActivity implements View.OnClickListener, MainContract.View {
    private static final String TAG = MainActivity.class.getName().split("app")[1];
    private static final int mRequestCode = 100;

    List<ImageView> mImageViewList = new ArrayList<>();

    SeekBar mSeekBar;
    ArrayMap mViewMap = new ArrayMap();
    MainPresenter mPresenter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        Log.d(TAG, "onCreate: DONE");
        mPresenter = new MainPresenter(this);
        mPresenter.start();
        mPresenter.setView(this);

        initView();
    }

    @SuppressLint("NewApi")
    private void initView() {
        ArrayList<String> UnauthorizedList = new ArrayList<>();
        UnauthorizedList.add("android.permission.INTERNET");

        // Requset Premission
        String[] array = UnauthorizedList.toArray(new String[UnauthorizedList.size()]);
        ActivityCompat.requestPermissions(
                this,
                array,
                mRequestCode
        );

        ImageView image_hello = findViewById(R.id.image_hello);
        ImageView image_area2 = findViewById(R.id.image_area2);

        image_hello.setZ(20.f);
        image_area2.setZ(10.f);

        mImageViewList.add(image_hello);
        mImageViewList.add(image_area2);
//        mObjectAnimationHelper = new ObjectAnimationHelper(mImageViewList);
        mSeekBar = findViewById(R.id.seek_progress);

        Button bt_add = findViewById(R.id.bt_add);
        bt_add.setOnClickListener(this);

        Button bt_switch = findViewById(R.id.bt_switch_position);
        bt_switch.setOnClickListener(this);

        Button bt_remote = findViewById(R.id.bt_remote);
        bt_remote.setOnClickListener(this);

        Button bt_thread = findViewById(R.id.bt_thread);
        bt_thread.setOnClickListener(this);

        // foreach View in MainActivity
        ArrayDeque queue = new ArrayDeque<>();
        queue.addLast(getWindow().getDecorView());
        while (!queue.isEmpty()) {
            View temp = (View) queue.getLast();
            //队尾出队
            queue.pollLast();
            if (temp instanceof ViewGroup) {
                int childCount = ((ViewGroup) temp).getChildCount();
                for (int i = childCount-1; i >= 0; i--) {
                    queue.addLast(((ViewGroup) temp).getChildAt(i));
                }
            }
            //业务：如果view设置了listener那么就设置Touch监听
            mViewMap.put(temp.getId()+"",temp);
        }
        mAnimationHelper = new AnimationHelper(mViewMap);
        Log.d(TAG, "initView: "+mViewMap.toString());

        Log.d(TAG, "initView: DONE");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: DONE");
    }

    AnimationHelper mAnimationHelper;

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_add:
                DownloadAsyncTask downloadAsyncTask =
                        new DownloadAsyncTask(
                                findViewById(R.id.text_loading),
                                mImageViewList.get(0),
                                findViewById(R.id.seek_progress)
                        );
                downloadAsyncTask.execute(String.valueOf(1000));
                break;
            case R.id.bt_switch_position:
//                mAnimationHelper.setAnimation();
//
                mAnimationHelper.setAnimaToChange();
//                setObjectAnima();
                break;
            case R.id.bt_remote:
                if(mPresenter != null){
                    try {
                        TextView text_loading = findViewById(R.id.text_loading);
                        text_loading.setText("Loading");
                        mPresenter.startDownload();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
               break;
            case R.id.bt_thread:
                Intent intent = new Intent(MainActivity.this, MusicWindowActivity.class);
                startActivity(intent);
                Log.d(TAG, "onClick: goTo new Activity");
                this.finish();
                break;
            default:
                break;
        }
    }


    @Override
    public void setPresenter(MainContract.Presenter presenter) {

    }

    @Override
    public void updateImage(Bitmap bitmap) {
        mImageViewList.get(1).setImageBitmap(bitmap);
        TextView text_loading = findViewById(R.id.text_loading);
        text_loading.setText("DONE");
        Log.d(TAG, "onDownloadDone: DONE");
    }

    @Override
    public void updateProgress(int progress) {
        mSeekBar.setProgress(progress);
    }
}